﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.OleDb;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace PookSistener.Customer
{
    public partial class frmCustomerAdd : Form
    {
        internal string MODE = "";

        OleDbConnection conn = null;

        public frmCustomerAdd()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.DialogResult = System.Windows.Forms.DialogResult.OK;
            this.Close();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (txtCustomerName.Text.Trim().Length > 0)
            {
                if (MODE.Equals("ADD"))
                {
                    string sql = @"INSERT INTO Customer(Name, Phone, Address) VALUES(@Name, @Phone, @Address)";
                    OleDbCommand cmd = new OleDbCommand();
                    cmd.CommandText = sql;
                    cmd.CommandType = CommandType.Text;
                    cmd.Connection = conn;
                    cmd.Parameters.Clear();

                    cmd.Parameters.Add("@Name", OleDbType.VarChar).Value = txtCustomerName.Text;
                    cmd.Parameters.Add("@Phoen", OleDbType.VarChar).Value = txtPhone.Text;
                    cmd.Parameters.Add("@Address", OleDbType.VarChar).Value = txtAddress.Text;

                    cmd.ExecuteNonQuery();

                    MessageBox.Show("เพิ่มข้อมูลเรียบร้อยแล้ว");
                    ClearData();
                }
                else if (MODE.Equals("EDIT"))
                {
                    try
                    {
                        int customer_id = Convert.ToInt32(txtCustomerId.Text);

                        string sql = @"UPDATE Customer SET Name=@Name, Phone=@Phone, Address=@Address WHERE ID=@ID";
                        
                        OleDbCommand cmd = new OleDbCommand();
                        cmd.CommandText = sql;
                        cmd.CommandType = CommandType.Text;
                        cmd.Connection = conn;
                        cmd.Parameters.Clear();

                        cmd.Parameters.Add("@Name", OleDbType.VarChar).Value = txtCustomerName.Text;
                        cmd.Parameters.Add("@Phoen", OleDbType.VarChar).Value = txtPhone.Text;
                        cmd.Parameters.Add("@Address", OleDbType.VarChar).Value = txtAddress.Text;
                        cmd.Parameters.Add("@ID", OleDbType.Integer).Value = txtCustomerId.Text;

                        cmd.ExecuteNonQuery();


                        MessageBox.Show("แก้ไขข้อมูลเรียบร้อยแล้ว");

                        this.Close();
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show("เกิดปัญหาบางอย่างขึ้น [" + ex.Message + "]");
                    }
                }
            }
        }

        private void ClearData()
        {
            txtCustomerName.Text = "";
            txtPhone.Text = "";
            txtAddress.Text = "";
            txtCustomerName.Focus();
            txtCustomerName.SelectAll();
        }

        private void frmCustomerAdd_Load(object sender, EventArgs e)
        {
            conn = new OleDbConnection(PookSistener.Properties.Settings.Default.DatabaseConnectionString);
            if (conn.State == ConnectionState.Open)
            {
                conn.Close();
            }
            conn.Open();
        }

        private void groupBox1_Enter(object sender, EventArgs e)
        {

        }

        private void txtCustomerId_TextChanged(object sender, EventArgs e)
        {

        }
    }
}
